//*****************************************************************************
// JFab (http://code.google.com/p/jfab)
// Copyright (c) 2011-2012 JFab.org
// Admin jfab@jeffreythompson.net
//
// See the file "LICENSE.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//*****************************************************************************

package org.jfab.swingui;

import java.awt.Component;

import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

import org.apache.commons.lang3.StringUtils;
import org.jfab.swingui.decorator.Decorator;

/**
 * Provides a tree cell renderer for a JFab tree.
 */
public class JFabTreeCellRenderer extends DefaultTreeCellRenderer
{
    /** Serial version UID. */
    private static final long serialVersionUID = 1L;

    @Override
    public Component getTreeCellRendererComponent(final JTree tree,
            final Object value, final boolean sel, final boolean expanded,
            final boolean leaf, final int row, final boolean hasFocusIn)
    {
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
                row, hasFocusIn);

        if (value instanceof Decorator)
        {
            final Decorator<?> decorator = (Decorator<?>)value;

            final String displayName = decorator.getDisplayName();

            if (StringUtils.isNotEmpty(displayName))
            {
                setText(displayName);
            }

            final Icon icon = decorator.getIcon();

            if (icon != null)
            {
                setIcon(icon);
            }
        }

        return this;
    }
}
